package com.naiterui.ehp.bs.emr.vo.health;

import cn.hutool.core.collection.CollectionUtil;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.date.DateUtil;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @Description 传染病史
 * @Author gongbaoqiang
 * @Date 2020/6/2 2:24 PM
 */
@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("传染病史")
public class InfectionVO implements Serializable {
    private static final long serialVersionUID = 8435074613563993516L;

    /**
     * 选中值
     */
    @ApiModelProperty("选中值")
    private List<String> values;

    /**
     * 肝炎详情
     */
    @ApiModelProperty("肝炎详情")
    private InfectionDetailVO hepatitis;

    /**
     * 结核详情
     */
    @ApiModelProperty("结核详情")
    private InfectionDetailVO tuberculosis;

    /**
     * 梅毒确诊日期
     */
    @ApiModelProperty("梅毒确诊日期")
    @JsonFormat(pattern = DateUtil.DATE_FORMAT)
    private Date syphilisDate;

    /**
     * 艾滋病确诊日期
     */
    @ApiModelProperty("艾滋病确诊日期")
    @JsonFormat(pattern = DateUtil.DATE_FORMAT)
    private Date aidsDate;

    /**
     * 其他详情
     */
    @ApiModelProperty("其他详情")
    private InfectionDetailVO other;

    /**
     * 选项明细
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class InfectionDetailVO implements Serializable {

        private static final long serialVersionUID = 746953346245925838L;
        /**
         * 类型
         */
        @ApiModelProperty("类型")
        private String type;

        /**
         * 确诊日期
         */
        @ApiModelProperty("确诊日期")
        @JsonFormat(pattern = DateUtil.DATE_FORMAT)
        private Date diagnosisDate;

        /**
         * 治愈后
         */
        @ApiModelProperty("治愈后")
        private String afterCure;

        /**
         * 治愈日期
         */
        @ApiModelProperty("治愈日期")
        @JsonFormat(pattern = DateUtil.DATE_FORMAT)
        private Date cureDate;

        public void validate() throws BusinessException {
            if (StringUtils.isBlank(this.type) || this.diagnosisDate == null || StringUtils.isBlank(this.afterCure)) {
                throw new BusinessException(ExceptionCodes.PATIENT_HEALTH_RECORD_INFECTION);
            }
            if ("治愈".equals(this.afterCure) && this.cureDate == null) {
                throw new BusinessException(ExceptionCodes.PATIENT_HEALTH_RECORD_INFECTION);
            }
        }


    }

    public void validate() throws BusinessException {
        if (CollectionUtil.isEmpty(this.values)) {
            throw new BusinessException(ExceptionCodes.PATIENT_HEALTH_RECORD_INFECTION);
        }
        if (this.values.contains("肝炎")) {
            this.hepatitis.validate();
        }
        if (this.values.contains("结核")) {
            this.tuberculosis.validate();
        }
        if (this.values.contains("梅毒")) {
            if (this.syphilisDate == null) {
                throw new BusinessException(ExceptionCodes.PATIENT_HEALTH_RECORD_INFECTION);
            }
        }
        if (this.values.contains("艾滋病")) {
            if (this.aidsDate == null) {
                throw new BusinessException(ExceptionCodes.PATIENT_HEALTH_RECORD_INFECTION);
            }
        }
        if (this.values.contains("其他")) {
            this.other.validate();
        }
    }

}
